import axios from 'axios';

export default {
    namespaced: true,
    state: {
        preOrderNo: null,
        orderInfo: null,
        payPalOpen: null,
        error: null,
    },
    mutations: {
        SET_PRE_ORDER_NO(state, preOrderNo) {
            state.preOrderNo = preOrderNo;
        },
        SET_ORDER_INFO(state, orderInfo) {
            state.orderInfo = orderInfo;
        },
        SET_PAYPAL_OPEN(state, payPalOpen) {
            state.payPalOpen = payPalOpen;
        },
        SET_ERROR(state, error) {
            state.error = error;
        },
    },
    actions: {
        async loadPreOrderDetails({ commit, state }) {
            try {
                const response = await axios.get(`order/api/order/load/pre/travel/${state.preOrderNo}`);
                if (response.data.code === 200) {
                    commit('SET_ORDER_INFO', response.data.data.orderInfoVo);
                    commit('SET_PAYPAL_OPEN', response.data.data.payPalOpen);
                    return { success: true };
                } else {
                    commit('SET_ERROR', response.data.message || '加载预下单失败');
                    return { success: false, error: response.data.message || '加载预下单失败' };
                }
            } catch (error) {
                console.error('加载预下单失败:', error);
                const errorMessage = '加载预下单失败，请稍后再试';
                commit('SET_ERROR', errorMessage);
                return { success: false, error: errorMessage };
            }
        },
    },
    getters: {
        getPreOrderNo: state => state.preOrderNo,
        getOrderInfo: state => state.orderInfo,
        getPayPalOpen: state => state.payPalOpen,
        getError: state => state.error,
    },
};
